package com.dotstone.chipism.adapter;

import java.util.ArrayList;
import java.util.List;

import com.dotstone.chipism.R;
import com.dotstone.chipism.bean.Device;
import com.dotstone.chipism.util.DeviceManager;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ChildDeviceAdapter extends BaseAdapter {
	private List<Device> childDevices = new ArrayList<Device>();
	private Context context;

	public ChildDeviceAdapter(Context context, List<Device> childDevices) {
		this.context = context;
		this.childDevices = childDevices;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return childDevices.size();
	}

	@Override
	public Object getItem(int p) {
		// TODO Auto-generated method stub
		return childDevices.get(p);
	}

	@Override
	public long getItemId(int p) {
		// TODO Auto-generated method stub
		return p;
	}

	@Override
	public View getView(int p, View v, ViewGroup viewgroup) {
		// TODO Auto-generated method stub
		if (v == null) {
			v = LayoutInflater.from(context).inflate(
					R.layout.item_child_device, null);
			holdView = new HoldView();
			holdView.name = (TextView) v.findViewById(R.id.name_tv);
			holdView.state = (TextView) v.findViewById(R.id.state_tv);
			holdView.mTypeImg = (ImageView) v.findViewById(R.id.type_img);
			v.setTag(holdView);
		} else {
			holdView = (HoldView) v.getTag();
		}
		Device d = childDevices.get(p);
		Log.i("wmy", "d.getDeviceName() = " + d.getDeviceName());
		holdView.name.setText(d.getDeviceName());
		if (DeviceManager.getInstance().checkDeviceState(d.getDeviceID())) {
			holdView.state.setText("开");
		} else {
			holdView.state.setText("关");
		}
		return v;
	}

	HoldView holdView;

	class HoldView {
		private TextView name, state;
		private ImageView mTypeImg;
	}

}